import { NzMessageService } from 'ng-zorro-antd/message';
import { MessageList } from './../../domains/messageList.domain';
import { Component, OnInit } from '@angular/core';
import { MessageServiceSvr } from 'src/app/service/message.service';
import { Message } from 'src/app/domains/message.domain';

@Component({
  selector: 'app-message-notification',
  templateUrl: './message-notification.component.html',
  styleUrls: ['./message-notification.component.less']
})


export class MessageNotificationComponent implements OnInit {

  constructor(
    private messageServiceSvr: MessageServiceSvr,
    private message: NzMessageService
  ) { }

  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  notifyTitle: string = ''
  state: number = 0
  page: number = 1
  size: number = -1
  // messageList: MessageList = new MessageList()
  messageList: any
  messageModel: boolean = false
  modalMessage: Message = new Message()

  ngOnInit(): void {
    this.getMessageList()
  }

  notifyData = [
    {
      id: 1,
      status: 0,
      title: '标题一',
      time: '2022.03.12',
    },
    {
      id: 1,
      status: 0,
      title: '标题一',
      time: '2022.03.12',
    },
    {
      id: 1,
      status: 0,
      title: '标题一',
      time: '2022.03.12',
    }
  ]

  // 获取消息列表
  async getMessageList() {
    let result = await this.messageServiceSvr.listMessages(this.token, this.notifyTitle, this.state, this.customerId, this.page, this.size)
    this.messageList = result._Items
    console.log(this.messageList)
  }

  // 查看消息详情
  async getMessageDetail(message: Message) {
    this.modalMessage = message
    this.messageServiceSvr.getMessageByPrimaryKey(this.token, message.Id)
    this.messageModel = true
    try {
      let res = await this.messageServiceSvr.updateMessageStatus(this.token, message.Id)
      console.log(1)
      if(res===1) {
        this.getMessageList()
      }
    } catch (error) {
      console.log(error)
    }

  }

  handleOk() {
    this.messageModel = false
  }

  handleOpen() {
  }

  close() {
  }

  async delMessage(messageId: string[]) {
    let result = await this.messageServiceSvr.deleteMessage(this.token, messageId)
    console.log(result)
    if(result===1) {
      this.message.success('该消息删除成功')
      this.getMessageList()
    } else {
      this.message.error(`${result}`)
    }
  }

}
